package com.loyal.mwm.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import androidx.appcompat.widget.Toolbar;

import com.base.kit.GsonUtil;
import com.base.kit.ResUtil;
import com.loyal.core.helper.Low2UpCase;
import com.loyal.mwm.R;
import com.loyal.mwm.adapter.AutoCompleteAdapter;
import com.loyal.mwm.adapter.PinnedCityAdapter;
import com.loyal.mwm.base.BaseSwipeActivity;
import com.loyal.mwm.beans.CityBean;
import com.loyal.mwm.databinding.ActivityCityBinding;
import com.loyal.mwm.util.ImageUtil;
import com.loyal.mwm.util.SortCity;
import com.loyal.mwm.util.ToastUtil;
import com.loyal.mwm.views.PinnedHeaderListView;
import com.loyal.mwm.views.SideBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class CityActivity extends BaseSwipeActivity<ActivityCityBinding> implements View.OnClickListener, SideBar.OnTouchingLetterChangedListener {
    /* @BindView(R.id.toolbar)
     Toolbar toolbar;
     @BindView(R.id.edit_cityName)
     AutoCompleteTextView editCity;
     @BindView(R.id.view_layout)
     View viewQuery;
     @BindView(R.id.query_cancel)
     AppCompatTextView textCancel;
     @BindView(R.id.text_city_query)
     AppCompatTextView textCity;
     @BindView(R.id.city_pinned_listView)
     PinnedHeaderListView cityPinnedView;
     @BindView(R.id.sidebar)
     SideBar sideBar;
     @BindView(R.id.sidebar_dialog)
     AppCompatTextView textSideBar;*/
    private PinnedCityAdapter pinnedCityAdapter;
    private LinkedHashMap<String, List<CityBean>> linkedHashMap = new LinkedHashMap<>();
    private AutoCompleteAdapter completeAdapter;

    @Override
    public int actLayoutRes() {
        return R.layout.activity_city;
    }

    @Override
    public void afterOnCreate() {
        binding.setDrawable(ImageUtil.getBackground(this));
        initViews();
        initData();
    }

    @Override
    public int setEdgePosition() {
        return LEFT;
    }

    private void initViews() {
        Toolbar toolbar = binding.toolbar.toolbar;
        toolbar.setTitle("选择城市");
        //toolbar.setBackground();
        //toolbar.setBackgroundColor();

        setSupportActionBar(toolbar);
        changeInput(false);
    }

    private void initData() {
        binding.editCityName.setAdapter(completeAdapter = new AutoCompleteAdapter());
        binding.cityPinnedListView.setAdapter(pinnedCityAdapter = new PinnedCityAdapter(this, linkedHashMap));
        String json = ResUtil.assetFile2String(this, "json/allCity.json");
        List<CityBean> beanList = GsonUtil.json2List(json, CityBean.class);
        Collections.sort(beanList, new SortCity());
        for (CityBean cityBean : beanList) {
            String letter = cityBean.getCityLetter().toUpperCase();
            List<CityBean> getList = linkedHashMap.get(letter);
            if (getList == null) {
                getList = new ArrayList<>();
                linkedHashMap.put(letter, getList);
            }
            getList.add(cityBean);
        }
        pinnedCityAdapter.refreshData(linkedHashMap);
        completeAdapter.notifyList(beanList);
        binding.sidebar.setTextView(binding.sidebarDialog);
        binding.sidebar.setOnTouchingLetterChangedListener(this);
        binding.cityPinnedListView.setOnItemClickListener(new ItemClickListener());
        binding.editCityName.setOnItemClickListener(onCompleteItemClick);
        binding.editCityName.setTransformationMethod(new Low2UpCase());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_city_query:
                changeInput(true);
                break;
            case R.id.query_cancel:
                changeInput(false);
                break;
            case R.id.view_layout:
                changeInput(false);
                break;
        }
    }

    private void changeInput(boolean isQuery) {
        binding.editCityName.setVisibility(isQuery ? View.VISIBLE : View.GONE);
        binding.textCityQuery.setVisibility(isQuery ? View.GONE : View.VISIBLE);
        binding.queryCancel.setVisibility(isQuery ? View.VISIBLE : View.GONE);
        binding.viewLayout.setVisibility(isQuery ? View.VISIBLE : View.GONE);
        /*adjustPan时不需要，adjustResize需要设置如下*/
        //sideBar.setVisibility(isQuery ? View.GONE : View.VISIBLE);
        if (isQuery) {
            ToastUtil.showInputPan(this);
            binding.editCityName.requestFocus();
        } else {
            binding.editCityName.getText().clear();
            UIHelper.hideKeyBoard(binding.editCityName);
        }
    }

    @Override
    public void onTouchingLetterChanged(String str) {
        Iterator<Map.Entry<String, List<CityBean>>> iterator = linkedHashMap.entrySet().iterator();
        int i = 0;
        while (true) {
            Map.Entry<String, List<CityBean>> localEntry;
            if (iterator.hasNext()) {
                localEntry = iterator.next();
                if ((localEntry.getKey()).equals(str)) {
                    binding.cityPinnedListView.setSelection(i);
                }
            } else {
                return;
            }
            i += 1 + localEntry.getValue().size();
        }
    }

    private AdapterView.OnItemClickListener onCompleteItemClick = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            List<CityBean> filterList = completeAdapter.getFilterList();
            CityBean cityBean = filterList.get(position);
            back2Last(cityBean);
        }
    };

    private class ItemClickListener extends PinnedHeaderListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int section, int position, long id) {
            CityBean cityBean = (CityBean) pinnedCityAdapter.getItem(section, position);
            back2Last(cityBean);
        }

        @Override
        public void onSectionClick(AdapterView<?> adapterView, View view, int section, long id) {
        }
    }

    private void back2Last(CityBean cityBean) {
        if (null == cityBean || TextUtils.isEmpty(cityBean.getCityName()))
            return;
        final String cityName = cityBean.getCityName();
        binding.editCityName.setText(cityName);
        binding.editCityName.postDelayed(() -> {
            Intent intent = new Intent();
            intent.putExtra("cityName", cityName);
            setResult(RESULT_OK, intent);
            finish();
        }, 500);
    }
}